package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.PictorialDao;
import cn.sumpu.app.guoguojie.domain.DoingPictorial;
import cn.sumpu.app.guoguojie.domain.DoingPictorialVisit;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class PictorialDaoImpl extends SqlSessionDaoSupport implements PictorialDao {

    private final static Logger logger = LoggerFactory.getLogger(PictorialDaoImpl.class);

    @Override
    public void insertPictorial(DoingPictorial pictorial) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.insertPictorial", pictorial);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Insert database failure.", e);
            }
            throw new AppBusinessException("Write database failure.", e);
        }
    }

    @Override
    public void updatePictorial(DoingPictorial pictorial) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.updatePictorial", pictorial);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("update database failure.", e);
            }
            throw new AppBusinessException("update database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<DoingPictorial> getPictorialList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    @Override
    public int getPictorialListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialListCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    @Override
    public int getExistPicpro(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getExistPicpro", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    @Override
    public void deleteDoingByPic(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.deleteDoingByPic", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("delete database failure.", e);
            }
            throw new AppBusinessException("delete database failure.", e);
        }
    }

    @Override
    public void deletePictorial(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.deletePictorial", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("delete database failure.", e);
            }
            throw new AppBusinessException("delete database failure.", e);
        }
    }

    @Override
    public void deletePictureByPic(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.deletePictureByPic", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("delete database failure.", e);
            }
            throw new AppBusinessException("delete database failure.", e);
        }
    }

    @Override
    public void deleteProductByPic(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.deleteProductByPic", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("delete database failure.", e);
            }
            throw new AppBusinessException("delete database failure.", e);
        }
    }

    //查询访问画报的人
    @SuppressWarnings("unchecked")
    @Override
    public List<DoingPictorialVisit> selectPictorialVisitList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.selectPictorialVisitList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    //查询访问画报的信息
    @Override
    public DoingPictorialVisit selectVisitor(DoingPictorialVisit doingPictorialVisit) throws AppBusinessException {
        try {
            return (DoingPictorialVisit) getSqlSession()
                .selectOne("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.selectVisitor", doingPictorialVisit);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("selectVisitor database failure.", e);
            }
            throw new AppBusinessException("selectVisitor database failure.", e);
        }
    }

    //更新已访问画报信息
    @Override
    public void updatePictorialVisit(DoingPictorialVisit doingPictorialVisit) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.updatePictorialVisit", doingPictorialVisit);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updatePictorialVisit database failure.", e);
            }
            throw new AppBusinessException("updatePictorialVisit database failure.", e);
        }
    }

    //插入访问画报的信息
    @Override
    public void insertPictorialVisit(DoingPictorialVisit doingPictorialVisit) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.insertPictorialVisit", doingPictorialVisit);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertPictorialVisit database failure.", e);
            }
            throw new AppBusinessException("insertPictorialVisit database failure.", e);
        }
    }

    //替换访问记录
    @Override
    public void updateNewVisit(DoingPictorialVisit doingPictorialVisit) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.updateNewVisit", doingPictorialVisit);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateNewVisit database failure.", e);
            }
            throw new AppBusinessException("updateNewVisit database failure.", e);
        }
    }

    //查询推荐的画报
    @SuppressWarnings("unchecked")
    @Override
    public List<DoingPictorial> getPictorialByChannel(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialByChannel", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getPictorialByChannel database failure.", e);
            }
            throw new AppBusinessException("getPictorialByChannel database failure.", e);
        }
    }

    //查询每个分类推荐的第一个
    @SuppressWarnings("unchecked")
    @Override
    public List<DoingPictorial> getHotPictorialNo1(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getHotPictorialNo1", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getHotPictorialNo1 database failure.", e);
            }
            throw new AppBusinessException("getHotPictorialNo1 database failure.", e);
        }
    }

    //查询推荐的画报的总记录数
    @Override
    public Integer getPictorialCountByChannel(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialCountByChannel", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getPictorialCountByChannel database failure.", e);
            }
            throw new AppBusinessException("getPictorialCountByChannel database failure.", e);
        }
    }

    //查询最新的画报
    @SuppressWarnings("unchecked")
    @Override
    public List<DoingPictorial> getPictorialNew(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialNew", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getPictorialNew database failure.", e);
            }
            throw new AppBusinessException("getPictorialNew database failure.", e);
        }
    }

    //查询最新的画报的总记录数
    @Override
    public Integer getPictorialCountNew(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialCountNew", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getPictorialCountNew database failure.", e);
            }
            throw new AppBusinessException("getPictorialCountNew database failure.", e);
        }
    }

    //查询前N条画报记录
    @SuppressWarnings("unchecked")
    @Override
    public List<DoingPictorial> getPictorialBruce(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<DoingPictorial>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingPictorialMapper.getPictorialBruce", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getPictorialBruce database failure.", e);
            }
            throw new AppBusinessException("getPictorialBruce database failure.", e);
        }
    }

}
